package com.example.model;

import android.health.connect.datatypes.units.Length;

import com.example.utils.Constants;

import java.util.Random;

public class Block {
    public int row = 0;
    public int column = 0;
    public int imageResource = 0;
    //记录当前方格图形
    private BlockType type;
    //记录当前的形状
    private int[][] shape;

    public void addColumn(){
        column += 1;
    }

    public void reduceColumn(){
        column -= 1;
    }

    public void addRow(){
        row += 1;
    }

    public void reduceRow(){
        row -= 1;
    }

    public void rotate(){
        if (type == BlockType.O)return;

        int n = shape.length;
        int[][] rotated = new int[n][n];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                rotated[j][n - 1 - i] = shape[i][j];
            }
        }

        shape = rotated;
    }

    public Block(int row, int column) {
        this.row = row;
        this.column = column;

        //获取随机图片资源
        imageResource = Constants.getRandomImageRes();

        //获取随机方块图形
        type = getRandomType();

        //得到type之后就可以得到形状的二维数组了
        shape = BlockShape.getShape(type);

    }
    private BlockType getRandomType(){
        //通过values数组拿到枚举里的值
        BlockType[] types = BlockType.values();
        Random random = new Random();
        int index = random.nextInt(types.length);

        return types[index];
    }

    //获取方块图形对于的二维数组
    public int [][] getShape(){
        return shape;
    }

    public enum BlockType{
        I,L,J,T,S,Z,O
    }
}
